//
//  MineViewController.m
//  yunyan
//
//  Created by haichuang on 2018/2/11.
//  Copyright © 2018年 haichuang. All rights reserved.
//

#import "MineViewController.h"
#import "MineTopCell.h"
//#import "MyCollectionViewController.h"
#import "ShMyCollectListViewController.h"
#import "MyPictureViewController.h"
#import "SettingViewController.h"
#import "MyCommentViewController.h"
#import "MyPraiseViewController.h"
//#import "RevisingPersonalDataViewController.h"
//#import "MailboxMessageViewController.h"
#import "EMSFeedBackViewController.h"
#import "ChangeUserViewController.h"

@interface MineViewController ()<UITableViewDelegate, UITableViewDataSource>{
    
    NSDictionary * dataDic;
}
@property (nonatomic, strong) NSArray * itemArr;//分类
@property (nonatomic, strong) UITableView * mineTab;
@end

@implementation MineViewController


-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
    
        NSString * user_id = [NSString stringWithFormat:@"%ld",[USER_DEFAULTGet(@"user_id")  integerValue]];

        if (user_id.length>1) {
            [self getUserDetailsRequest];
            
        }else {
//            LoginViewController *loginVC = [LoginViewController new];
//            UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:loginVC];
//            [self presentViewController:navc animated:YES completion:nil];
        }
}

-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    WeakSelf;
    
    
    self.mineTab = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    if (@available(iOS 11.0, *)) {
        self.mineTab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.mineTab.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.mineTab];
    [self.mineTab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo (weakSelf.view);
    }];

    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        NSString * user_id = [NSString stringWithFormat:@"%ld",[USER_DEFAULTGet(@"user_id")  integerValue]];
        if (user_id.length>1) {
            [self getUserDetailsRequest];
        }else {
            LoginViewController *loginVC = [LoginViewController new];
            UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:loginVC];
            [self presentViewController:navc animated:YES completion:nil];
        }

    }];
    
    
    self.mineTab.delegate = self;
    self.mineTab.dataSource = self;
    
    _itemArr = @[@"我的评论",@"我的点赞",@"信箱留言",@"系统设置"];
    
    [self.mineTab registerClass:[MineTopCell class] forCellReuseIdentifier:@"MineTopCell"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableview delegete;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 5;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
//    if (section == 0) {
//        return 1;
//    }
    
      return 1;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return 10.f;
    }else{
    return CGFLOAT_MIN;
    }
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 270;
    }
    return 50;
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WeakSelf;
    
    if (indexPath.section==0) {
        
        MineTopCell * cell1=[tableView dequeueReusableCellWithIdentifier:@"MineTopCell" forIndexPath:indexPath];
        
        cell1.selectionStyle = UITableViewCellSelectionStyleNone;
        //头像
        [cell1.userImage sd_setImageWithURL:ImageUrl(dataDic[@"head_url"]) placeholderImage:ImageName(@"img_head_portrait_pre")];
        cell1.nameLabel.text = dataDic[@"user_nick"];
        
        cell1.imageBlock = ^(void){
            
            
            NSString * user_id = [NSString stringWithFormat:@"%ld",[USER_DEFAULTGet(@"user_id")  integerValue]];
            
            if (user_id.length>1) {
                
                 [weakSelf JumpChangeImage];
                
            }else {
                LoginViewController *loginVC = [LoginViewController new];
                UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:loginVC];
                [self presentViewController:navc animated:YES completion:nil];
            }
          
        };

        //收藏
        cell1.collectBtnBlock = ^(void){
            
            NSString * user_id = [NSString stringWithFormat:@"%ld",[USER_DEFAULTGet(@"user_id")  integerValue]];
            
            if (user_id.length>1) {
                 [weakSelf JumpCollection];
                
            }else {
                LoginViewController *loginVC = [LoginViewController new];
                UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:loginVC];
                [self presentViewController:navc animated:YES completion:nil];
            }
            
          

        };
        //随手拍
        cell1.pictureBtnBlock = ^(void){
            
            NSString * user_id = [NSString stringWithFormat:@"%ld",[USER_DEFAULTGet(@"user_id")  integerValue]];
            
            if (user_id.length>1) {
               [weakSelf JumpPicture];
                
            }else {
                LoginViewController *loginVC = [LoginViewController new];
                UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:loginVC];
                [self presentViewController:navc animated:YES completion:nil];
            }
            
            
        };
        //分享
        cell1.shareBtnBlock = ^(void){
            
            NSString * user_id = [NSString stringWithFormat:@"%ld",[USER_DEFAULTGet(@"user_id")  integerValue]];
            
            if (user_id.length>1) {
                 [weakSelf JumpShare];
                
            }else {
                LoginViewController *loginVC = [LoginViewController new];
                UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:loginVC];
                [self presentViewController:navc animated:YES completion:nil];
            }
            
        };
        
        
        return cell1;
        
    }else{
    
    
    UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    
    if (cell2 == nil) {
        cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CELL"];
       
    }
        cell2.textLabel.text = _itemArr[indexPath.section -1];
        cell2.textLabel.font = [UIFont systemFontOfSize:13.f];
        cell2.textLabel.textColor = RGBA(51, 51, 51, 1);
        cell2.selectionStyle = UITableViewCellSelectionStyleNone;
        cell2.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell2.imageView.image = [UIImage imageNamed:@"my_btn_default"];
    return  cell2;
    
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger section = indexPath.section;
 //   NSInteger row = indexPath.row;
    
    
    NSString * user_id = [NSString stringWithFormat:@"%ld",[USER_DEFAULTGet(@"user_id")  integerValue]];
    if (user_id.length>1) {
      
        
        UIViewController *destinationVC = nil;
        
        
        switch (section ) {
            case 0:
                
                break;
            case 1:
                destinationVC = [[MyCommentViewController alloc] init];
                
                break;
            case 2:
                destinationVC = [[MyPraiseViewController alloc] init];
                break;
            case 3:
            {
                EMSFeedBackViewController * mailVC = [[EMSFeedBackViewController alloc]init];
                destinationVC = mailVC;
//                mailVC.isMailbox = YES;
            }
                break;
            case 4:
            {
                SettingViewController * setVC = [[SettingViewController alloc]init];
                destinationVC = setVC;
            }
                break;
            default:
                break;
        }
        
        destinationVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:destinationVC animated:YES];
        
    }else {
        LoginViewController *loginVC = [LoginViewController new];
        UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:navc animated:YES completion:nil];
    }
    
   
}



#pragma mark - btnJump

-(void)JumpChangeImage{
    
    ChangeUserViewController * imageVC = [[ChangeUserViewController alloc]init];
    imageVC.userDic = dataDic;
    imageVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:imageVC animated:YES];
    
}
-(void)JumpCollection{
    
    ShMyCollectListViewController * collectionVC = [[ShMyCollectListViewController alloc]init];
        collectionVC.isShare = NO;
    collectionVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:collectionVC animated:YES];
    
}

-(void)JumpPicture{
    
    MyPictureViewController * pictureVC = [[MyPictureViewController alloc]init];
    pictureVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:pictureVC animated:YES];
    
    
}

-(void)JumpShare{
    
    ShMyCollectListViewController * collectionVC = [[ShMyCollectListViewController alloc]init];
    collectionVC.isShare = YES;
    collectionVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:collectionVC animated:YES];
}

//获取详情
-(void)getUserDetailsRequest{
    
    NSString * user_id = [NSString stringWithFormat:@"%ld",[USER_DEFAULTGet(@"user_id")  integerValue]];
    if (user_id.length>1) {
      
    }else {
        
        user_id = @"";
    }
    
    NSDictionary * dic = @{@"token":USER_DEFAULTGet(@"token"),
                           @"user_id":user_id,
                           @"mac_code":[SHJudgeTools getDeviceId]
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_userMessage_api) andparameters:dic Success:^(id responseObject) {
        
        dataDic = responseObject;
        
        NSLog(@"responseObject = %@",responseObject);
        
        USER_DEFAULTSet(dataDic[@"head_url"], @"head_url");
        USER_DEFAULTSet(dataDic[@"user_nick"], @"user_nick");
        
        [_mineTab reloadData];
        [_mineTab.mj_header endRefreshing];
        
    } failed:^(NSURLSessionDataTask * _Nullable task,NSError *error) {
       [_mineTab.mj_header endRefreshing];
        NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
        if (responses.statusCode == 404) {
            SHuiMBhudTextTimeStr(@"未获取到数据");
        }else if (responses.statusCode == 403){
            
            LoginViewController * login = [[LoginViewController alloc]init];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:login];
            [UIApplication sharedApplication].keyWindow.rootViewController = nav;
            
        }else {
            
            SHuiMBhudTextNetError;
        }
    }];
    
}

@end
